#!/bin/bash

CURPATH="/iflytek/uoamp/uoampagent"
CRONPATH="/var/spool/cron/root"

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:${CURPATH}/conf/thirdLibs
export LANG=en_US.UTF-8

. /etc/profile
if [ -e "~/.bash_profile" ];then
	. ~/.bash_profile
elif [ -e "~/.profile" ];then
	. ~/.profile
fi


chmod 700 -R ${CURPATH}
FLAGEID=$(ps aux | grep uoamp-agent-main |grep -v grep|grep -v uoampagent.sh|grep jar| awk '{print $2}')

if [ "$1" == "start" ];then
        if [ "$FLAGEID" == "" ];then
                exec java -jar -Xms2048m -Xmx2048m -XX:PermSize=128M -XX:MaxPermSize=2048m uoamp-agent-main-0.0.1-SNAPSHOT.jar >/dev/null 2>&1 &
        fi
         
elif [ "$1" == "stop" ];then
	IDS=$(ps aux | grep "uoamp-agent-main" |grep -v grep | grep -v guardMCAgent | awk '{print $2}' | head -n 1) 
	while [[ "$IDS" != "" ]]
	do  
    		kill -9 "$IDS"
    		IDS=$(ps aux | grep "uoamp-agent-main" |grep -v grep | grep -v guardMCAgent | awk '{print $2}' | head -n 1) 
	done  
elif [ "$1" == "restart" ];then
        echo "end restart"
	IDS=$(ps aux | grep "uoamp-agent-main" |grep -v grep | grep -v guardMCAgent | awk '{print $2}' | head -n 1)
        while [[ "$IDS" != "" ]]
        do
                kill -9 "$IDS"
                IDS=$(ps aux | grep "uoamp-agent-main" |grep -v grep | grep -v guardMCAgent | awk '{print $2}' | head -n 1)
        done
        exec java -jar -Xms2048m -Xmx2048m -XX:PermSize=128M -XX:MaxPermSize=2048m uoamp-agent-main-0.0.1-SNAPSHOT.jar >/dev/null 2>&1 &
fi
  
if [ -x "/etc/rc.local" ];
then
    echo "continue....." >> ./redeploy.log
else
    chmod +x /etc/rc.local
fi

if cat /etc/rc.local | grep './uoampagent.sh start' > /dev/null;
then
    echo "shell exists!" >> ./redeploy.log
elif cat /etc/rc.local | grep 'exit 0' | grep -v "#" > /dev/null;
    then
        sed -i 's/exit 0/cd \/iflytek\/uoamp\/uoampagent\//g' /etc/rc.local
        echo "./uoampagent.sh start" >> /etc/rc.local
        echo "exit 0" >> /etc/rc.local
        echo "add exit 0 to rc.load success!" >> ./redeploy.log
    else
        echo "cd $CURPATH" >> /etc/rc.local
        echo "./uoampagent.sh start" >> /etc/rc.local
        echo "add to rc.local success!" >> ./redeploy.log
fi
